#include "FunctionCode.h"

QString CFunctionCode::GetName(int iFunctionCode)
{
    QString ret = "unknown";
    if (READ_01_COIL == iFunctionCode) { //读线圈
        ret = "read_coil";
    }else if (READ_02_INPUT == iFunctionCode) { //读输入离散量
        ret = "read_input";
    }else if (READ_03_HOLD_REGISTERS == iFunctionCode) { //读保持寄存器
        ret = "read_hold_registers";
    }else if (READ_04_INPUT_REGISTERS == iFunctionCode) { //读输入寄存器
        ret = "read_input_registers";
    }else if (WRITE_05_ONE_COIL == iFunctionCode) { //写单个线圈
        ret = "write_one_col";
    }else if (WRITE_06_ONE_HOLD_REGISTER == iFunctionCode) { //写单个保持寄存器
        ret = "write_on_hold_register";
    }else if (WRITE_10_SOME_HOLD_REGISTERS == iFunctionCode) { //写多个保持寄存器
        ret = "write_some_hold_registers";
    }else
    {
        //todo
    }

    return ret;
}

bool CFunctionCode::IsRead(int iFunctionCode)
{
    return IsReadCoil(iFunctionCode)
           || IsReadInput(iFunctionCode)
           || IsReadHoldRegisters(iFunctionCode)
           || IsReadInputRegisters(iFunctionCode);
}

bool CFunctionCode::IsReadCoil(int iFunctionCode)
{
    return READ_01_COIL == iFunctionCode;
}

bool CFunctionCode::IsReadInput(int iFunctionCode)
{
    return READ_02_INPUT == iFunctionCode;
}

bool CFunctionCode::IsReadHoldRegisters(int iFunctionCode)
{
    return READ_03_HOLD_REGISTERS == iFunctionCode;
}

bool CFunctionCode::IsReadInputRegisters(int iFunctionCode)
{
    return READ_04_INPUT_REGISTERS == iFunctionCode;
}

bool CFunctionCode::IsWrite(int iFunctionCode)
{
    return IsWriteOneCoil(iFunctionCode)
           || IsWriteOneHoldRegister(iFunctionCode)
           || IsWriteSomeHoldRegisters(iFunctionCode);
}

bool CFunctionCode::IsWriteOneCoil(int iFunctionCode)
{
    return WRITE_05_ONE_COIL == iFunctionCode;
}

bool CFunctionCode::IsWriteOneHoldRegister(int iFunctionCode)
{
    return WRITE_06_ONE_HOLD_REGISTER == iFunctionCode;
}

bool CFunctionCode::IsWriteSomeHoldRegisters(int iFunctionCode)
{
    return WRITE_10_SOME_HOLD_REGISTERS == iFunctionCode;
}
